<template>
  <div class="addform">
    <h1>修改个人信息</h1>
    <br />
    <!-- 表单 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
      v-loading="loading"
    >
      <el-form-item label="是否编辑:">
        <el-switch v-model="ifEdit"></el-switch>
      </el-form-item>
      <el-form-item label="姓名:" prop="piName" label-width="100px">
        <el-input
          v-model="ruleForm.piName"
          class="inputS"
          :disabled="!ifEdit"
        ></el-input>
      </el-form-item>
      <el-form-item label="专业方向:" prop="piPd" label-width="100px">
        <el-input
          v-model="ruleForm.piPd"
          class="inputS"
          :disabled="!ifEdit"
        ></el-input>
      </el-form-item>
      <el-form-item label="自我介绍主段:" prop="piSiFirst">
        <el-input
          type="textarea"
          v-model="ruleForm.piSiFirst"
          :rows="6"
          class="inputL"
          :disabled="!ifEdit"
        ></el-input>
      </el-form-item>
      <el-form-item label="自我介绍副段:" prop="piSiSecond">
        <el-input
          type="textarea"
          v-model="ruleForm.piSiSecond"
          :rows="4"
          class="inputL"
          :disabled="!ifEdit"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          :disabled="!ifEdit"
        >
          修改信息
        </el-button>
        <el-button @click="resetForm('ruleForm')" :disabled="!ifEdit">
          重置
        </el-button>
      </el-form-item>
      <!-- 修改成功提示 -->
    </el-form>
    <!-- 成功提示框 -->
    <el-dialog
      title="温馨提示"
      :visible.sync="successVisible"
      width="20%"
      @close="refr"
      append-to-body
    >
      <div class="dialogSuc">
        <el-button
          type="success"
          icon="el-icon-check"
          circle
          @click="successVisible"
        ></el-button>
        <h2>修改成功</h2>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      successVisible: false,//成功提示
      ifEdit: false,//是否可以修改
      ruleForm: {},
      rules: {//如果没输入的提示信息
        piName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 5, message: "最大长度5个字,最小长度2个字", trigger: "blur" },
        ],
        piPd: [
          { required: true, message: "请输入专业方向", trigger: "blur" },
          { max: 20, message: "最大长度20个字", trigger: "blur" },
        ],
        piSiFirst: [
          { required: true, message: "请输入主段文字", trigger: "blur" },
          { max: 300, message: "最大长度300个字", trigger: "blur" },
        ],
        piSiSecond: [
          { required: true, message: "请输入副段文字", trigger: "blur" },
          { max: 200, message: "最大长度200个字", trigger: "blur" },
        ],
      },
    };
  },
  created() {// 提交方法 先判断提交表单是否有空=>validate
    console.log("进入修改个人信息页面")
    const _this = this;
    this.axios({
      method: "get",
      url: _this.baseUrl + "/index/get/indexPi"
    }).then(function (resp) {
      console.log(resp.data);
      _this.ruleForm = resp.data[0];
      _this.loading = false;
    });
  },
  methods: {
    // 修改按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 由于axios内部必须使用_this才能使用this
          const _this = this;
          this.axios({
            method: "post",
            url: _this.baseUrl + "/index/post/editPi",
            //传递给后端的数据
            data: _this.ruleForm
          }).then(function (resp) {
            console.log(resp.data);
            _this.successVisible = true;//提交成功提示
          });
        } else {
          console.log("修改失败");
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 刷新页面
    refr() {
      location.reload();
    },
  },
};
</script>

<style lang="less" scoped>
.addform {
  width: 80%;
  height: 100%;
  margin: 0 auto;
  .inputS {
    width: 60%;
  }
  .inputL {
    width: 70%;
    display: block;
  }
}
.dialogSuc {
    display: block;
    margin: 0 auto;
    width: 50%;
    button {
      display: block;
      margin: 0 auto;
    }
    h2 {
      color: green;
      text-align: center;
    }
  }
</style>